﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using Think.Lenovo.Com.Models;
using MySqlHelper = Think.Lenovo.Com.DataAccess.MysqlHelper;
namespace Think.Lenovo.Com.DataAccess
{
    public class feedbackInfo
    {
        public long feedbackAdd(feedback fback)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            //sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("insert into ")
                .Append("tbl_feedback (newsid, userName, feedbackConent,feedbackTime)")
                .Append("values(@newsid,@userName,@feedbackConent,@feedbackTime)");


            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@newsid",MySqlDbType.Int32),
                    new MySqlParameter("@userName",MySqlDbType.VarChar,50),
                    new MySqlParameter("@feedbackConent",MySqlDbType.VarChar,500),
                    new MySqlParameter("@feedbackTime",MySqlDbType.DateTime)
                };

            ps[0].Value = fback.NewsId;
            ps[1].Value = fback.UserName;
            ps[2].Value = fback.FeedBackContent;
            ps[3].Value = fback.FeedBackTime;

            long id = MysqlHelper.Insert(conn, sql.ToString(), ps);
            return id;
        }

        public void Deletefeedback(int id)
        {
            #region //信息

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            #region//sql语句
            string sql = "delete from tbl_feedback Where Id=@Id";

            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@id",MySqlDbType.Int32)
            };
            ps[0].Value = id;
            MysqlHelper.Delete(conn, sql.ToString(), ps);
            #endregion
            #endregion
        }

        /// <summary>
        /// 得到一页数据
        /// </summary>
        /// <param name="pageIndex">页号</param>
        /// <param name="pageSize">每页多少条数据</param>
        /// <param name="conditions">查询条件</param>
        /// <param name="orderBy">排序方式 </param>
        /// <returns></returns>
        public DataSet GetFeedbackData(int pageIndex, int pageSize, String conditions, String orderBy)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(128);
            sql.Append("Select * From  tbl_feedback ");
            if (!String.IsNullOrEmpty(conditions))
            {
                sql.Append(" Where " + conditions + " ");
            }
            if (!String.IsNullOrEmpty(orderBy))
            {
                sql.Append(" Order By " + orderBy + " ");
            }
            sql.Append(" Limit " + (pageIndex - 1) * pageSize + "," + pageSize);
            DataSet ds = MySqlHelper.Query(conn, sql.ToString());
            return ds;
        }

        /// <summary>
        /// 得到数据条数
        /// </summary>
        /// <param name="conditions">查询条件</param>
        /// <returns></returns>
        public int GetFeedbackCount(String conditions)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(128);

            sql.Append("Select Count(Id) From tbl_feedback ");
            if (!String.IsNullOrEmpty(conditions))
            {
                sql.Append(" Where "+conditions);
            }
           Object obj = MysqlHelper.ExecuteScalar(conn, sql.ToString());
           return obj == null ? 0 : Convert.ToInt32(obj);
        }
    }
}
